package com.example.qxfw.common.dmgis.analysis;


import com.dmgis.common.*;
import com.dmgis.dtm.Grd;
import com.dmgis.dtm.Kig;
import com.dmgis.dtm.Tin;
import com.dmgis.work.*;
import com.example.qxfw.common.dmgis.constant.ContourMapType;
import com.example.qxfw.common.dmgis.constant.RctType;
import com.example.qxfw.common.dmgis.vo.AInfGradeVo;
import com.example.qxfw.common.dmgis.vo.PointVo;
import com.example.qxfw.common.util.DmgisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;

@Component
public class GridAnalyse {

    @Value("${dmgis.tempPath}")
    private  String tempPath;



    static {
        //加载类库
        System.loadLibrary("dmjavaclass");
    }


    /**
     * 三角面插值生成面
     * @param points  站点数据
     * @param gridPath  grid地址
     * @param areaPath  等值面保存地址
     * @param markTextPath  标注文本保存地址
     * @param saveGridPath    插入之后的grid保存地址
     * @param aInfGradeVos  等值图分级
     */
    public  void tin(List<PointVo> points , String gridPath, String areaPath, String markTextPath, String saveGridPath, List<AInfGradeVo> aInfGradeVos){
        // 设置临时路径（必设）
        DmMapCom dmMapCom = new DmMapCom();
        dmMapCom.setTempPath(tempPath);

        VecWorkShop vecWorkShop=new VecWorkShop();
        if (points==null|| points.isEmpty()){
            return;
        }

        //三角网插值
        Tin tin=new Tin();
        points.forEach(point-> tin.addpoint(point.getX(),point.getY(),point.getVal()));

        //打开Grid工作区
        DtmWorkShop dtmWorkShop = new DtmWorkShop();
        short ai = dtmWorkShop.OpenGridWork(gridPath);
        DtmGridWork gridwork = (DtmGridWork) dtmWorkShop.GetWorkByNo(ai);
        if (gridwork==null){
            return ;
        }


        GridDataInfo dataInfo=new GridDataInfo();
        gridwork.GetGetGridInfo(dataInfo);

        Grd grd = new Grd();
        grd.setgridwork(gridwork);


        //获取需要插值的矩形范围
        Rct prc = new Rct();
        prc.Setxmax(dataInfo.GetxMax());
        prc.Setxmin(dataInfo.GetxMin());
        prc.Setymax(dataInfo.GetyMax());
        prc.Setymin(dataInfo.GetyMin());

        //计算四个最近点的值
        tin.calculate4pintzvalue(prc);

        //构建三角网
        tin.dmmesh();

        //添加三角网插值
        grd.addcontour(tin,1,1);
        gridwork.SaveWork(saveGridPath);

        //刷新Z值
        gridwork.UpdateZMaxMin();

        //添加等值面分级值
        DmgisUtil.classification(grd,aInfGradeVos);

        //设置等值线数值
        grd.pushcontour();

        //设置圆滑度
        grd.setsleek(5);

        //生成等值线
        grd.producecontourline((short) 0, 5);

        //等值线处理（得到父线，生成拓扑圈）
        grd.contourhandling();

        //获取等值面
        int workno = grd.getareawork(areaPath, vecWorkShop);
        AreaWork areaWork = (AreaWork) vecWorkShop.GetWorkByNo((short) workno);
        areaWork.SaveWork();

        //获取标记文本
        int textWorkNo = grd.gettextwork(markTextPath, vecWorkShop);
        TextWork textWork =(TextWork) vecWorkShop.GetWorkByNo((short) textWorkNo);
        textWork.SaveWork();

        dataInfo.Delete();
        tin.delete();
        grd.delete();
        vecWorkShop.delete();
        dtmWorkShop.delete();
    }


    /**
     * 克里金插值生成等值面
     * @param points  站点数据
     * @param gridPath  grid地址
     * @param areaPath  等值面保存地址
     * @param markTextPath  标注文本保存地址
     * @param saveGridPath    插入之后的grid保存地址
     * @param aInfGradeVos  等值图分级
     */
    public void kig(List<PointVo> points ,String gridPath, String areaPath,String markTextPath,String saveGridPath, List<AInfGradeVo> aInfGradeVos){
        //设置临时路径
        DmMapCom dmMapCom = new DmMapCom();
        dmMapCom.setTempPath(tempPath);

        //克里金插值生成等值面
        VecWorkShop vecWorkShop=new VecWorkShop();

        Kig kig = new Kig((short) 0);
        points.forEach(point-> kig.addinputdot(point.getX(),point.getY(),point.getVal()));
        //打开grid工作区
        DtmWorkShop dtmWorkShop = new DtmWorkShop();
        short ai = dtmWorkShop.OpenGridWork(gridPath);
        DtmGridWork gridwork = (DtmGridWork) dtmWorkShop.GetWorkByNo(ai);

        //设置Grid分析类
        Grd grd = new Grd();
        grd.setgridwork(gridwork);


        //获取Grid范围并计算行列距
        GridDataInfo gridDataInfo = new GridDataInfo();
        gridwork.GetGridInfo(gridDataInfo);
        double deltX = (gridDataInfo.GetxMax() - gridDataInfo.GetxMin()) / (gridDataInfo.Getcols() - 1);
        double deltY = (gridDataInfo.GetyMax() - gridDataInfo.GetyMin()) / (gridDataInfo.Getrows() - 1);

        //克里金插值处理过程
        kig.dealinputdot((short) 0);
        kig.setmodel(0);
        kig.setranglerect(gridDataInfo.GetxMin(), gridDataInfo.GetyMin(), gridDataInfo.GetxMax(), gridDataInfo.GetyMax());
        kig.setgridsize(deltX, deltY);
        kig.interpolation();
        grd.addkriging(kig, 1, 1);
        gridwork.UpdateZMaxMin();

        gridwork.GetGridInfo(gridDataInfo);
        grd.setgridwork(gridwork);
        //保存工作区
        gridwork.SaveWork(saveGridPath);

        //添加等值面分级值
        DmgisUtil.classification(grd,aInfGradeVos);

        //设置等值线数值
        grd.pushcontour();
        //设置圆滑度
        grd.setsleek(5);
        //生成等值线
        grd.producecontourline((short) 0, 1.5);
        //等值线处理（得到父线、生成拓扑圈）
        grd.contourhandling();
        int workno = grd.getareawork(areaPath, vecWorkShop);
        AreaWork areaWork = (AreaWork) vecWorkShop.GetWorkByNo((short) workno);
        areaWork.SaveWork();

        //获取标记文本
        int textWorkNo = grd.gettextwork(markTextPath, vecWorkShop);
        TextWork textWork =(TextWork) vecWorkShop.GetWorkByNo((short) textWorkNo);
        textWork.SaveWork();

        gridDataInfo.Delete();
        grd.delete();
        kig.delete();
        dtmWorkShop.delete();
        vecWorkShop.delete();
    }
}
